#-*- coding:utf-8 -*-
# Author: Liu Huan
# Mail: liuhuan@mail.las.ac.cn
# Datetime: 2019/7/31 16:06


import pandas as pd
import re


if __name__ == '__main__':
    df = pd.read_csv('arxiv_structured.csv')

    df_dict = {'id':[],'move':[],'text':[]}

    move_type_merge = {
        'background:':'BACKGROUND',
        'background :':'BACKGROUND',
        'background.':'BACKGROUND',
        'context:':'BACKGROUND',
        'context :':'BACKGROUND',
        'context.':'BACKGROUND',
        'aims:':'OBJECTIVE',
        'aims :':'OBJECTIVE',
        'aims.':'OBJECTIVE',
        'aim:':'OBJECTIVE',
        'aim :':'OBJECTIVE',
        'aim.':'OBJECTIVE',
        'purpose:':'OBJECTIVE',
        'purpose :':'OBJECTIVE',
        'purpose.':'OBJECTIVE',
        'methods:':'METHODS',
        'methods :':'METHODS',
        'methods.':'METHODS',
        'method:':'METHODS',
        'method :':'METHODS',
        'method.':'METHODS',
        'results:':'RESULTS',
        'results :':'RESULTS',
        'results.':'RESULTS',
        'result:':'RESULTS',
        'result :':'RESULTS',
        'result.':'RESULTS',
        'conclusions:':'CONCLUSIONS',
        'conclusions :':'CONCLUSIONS',
        'conclusions.':'CONCLUSIONS',
        'conclusion:':'CONCLUSIONS',
        'conclusion :':'CONCLUSIONS',
        'conclusion.':'CONCLUSIONS',
    }

    for i in range(len(df)):
        id_ = df.iloc[i]['ID']
        abst = df.iloc[i]['abstracts']
        if 'Aims.' in abst and 'Methods:' in abst:
            continue

        elif 'Context:' in abst or 'Aims:' in abst or 'Aim:' in abst:

            ## 第一种模式
            moves = []
            for m in ['Context:','Aims:','Aim:','Methods:','Method:','Results:','Result:','Conclusions:','Conclusion:']:
                if m in abst:
                    moves.append(m)

            abst_split = re.split(r"Context:|Aims?:|Methods?:|Results?:|Conclusions?:",abst)[1:]

            try:
                assert len(moves) == len(abst_split)
            except:
                print(i)
                print(abst)
                print(abst_split)
                print(moves)
                exit()
                
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)

        elif 'Context :' in abst or 'Aims :' in abst or 'Aim :' in abst:

            ## 第二种模式
            moves = []
            for m in ['Context :','Aims :','Aim :','Methods :','Method :','Results :','Result :','Conclusions :','Conclusion :']:
                if m in abst:
                    moves.append(m)
        
            abst_split = re.split(r"Context :|Aims? :|Methods? :|Results? :|Conclusions? :",abst)[1:]

            assert len(moves) == len(abst_split)
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)

        elif 'Background:' in abst or 'Purpose:' in abst:
            # 第三种模式
            moves = []
            for m in ['Background:','Purpose:','Methods:','Method:','Results:','Result:','Conclusions:','Conclusion:']:
                if m in abst:
                    moves.append(m)
            
            abst_split = re.split(r"Background:|Purpose:|Methods?:|Results?:|Conclusions?:",abst)[1:]

            assert len(moves) == len(abst_split)
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)

        elif 'Background :' in abst or 'Purpose :' in abst:
            ## 第四种模式
            moves = []
            for m in ['Background :','Purpose :','Methods :','Method :','Results :','Result :','Conclusions :','Conclusion :']:
                if m in abst:
                    moves.append(m)
            
            abst_split = re.split(r"Background :|Purpose :|Methods? :|Results? :|Conclusions? :",abst)[1:]

            try: 
                assert len(moves) == len(abst_split)
            except:
                print(abst)
                print(abst_split)
                print(moves)
                exit()
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)


        elif 'Context.' in abst or 'Aims.' in abst or 'Aim.' in abst:
            ## 第五种模式
            moves = []
            for m in ['Context.','Aims.','Aim.','Methods.','Method.','Results.','Result.','Conclusions.','Conclusion.']:
                if m in abst:
                    moves.append(m)
            
            abst_split = re.split(r"Context\.|Aims?\.|Methods?\.|Results?\.|Conclusions?\.",abst)[1:]

            
            try: 
                assert len(moves) == len(abst_split)
            except:
                print(abst)
                print(abst_split)
                print(moves)
                exit()
                
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)

        elif 'Background.' in abst or 'Purpose.' in abst:

            ## 第六种模式
            moves = []
            for m in ['Background.','Purpose.','Methods.','Method.','Results.','Result.','Conclusions.','Conclusion.']:
                if m in abst:
                    moves.append(m)

            abst_split = re.split(r"Background\.|Purpose\.|Methods?\.|Results?\.|Conclusions?\.",abst)[1:]

            assert len(moves) == len(abst_split)
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)


        elif 'BACKGROUND:' in abst or 'PURPOSE:' in abst:
            ## 第七种模式
            moves = []
            for m in ['BACKGROUND:','PURPOSE:','METHODS:','METHOD:','RESULTS:','RESULT:','CONCLUSIONS:','CONCLUSION:']:
                if m in abst:
                    moves.append(m)
            
            abst_split = re.split(r"BACKGROUND:|PURPOSE:|METHODS?:|RESULTS?:|CONCLUSIONS?:",abst)[1:]

            assert len(moves) == len(abst_split)
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)


        elif 'BACKGROUND :' in abst or 'PURPOSE :' in abst:
            ## 第八种模式
            moves = []
            for m in ['BACKGROUND :','PURPOSE :','METHODS :','METHOD :','RESULTS :','RESULT :','CONCLUSIONS :','CONCLUSION :']:
                if m in abst:
                    moves.append(m)

            abst_split = re.split(r"BACKGROUND :|PURPOSE :|METHODS? :|RESULTS? :|CONCLUSIONS? :",abst)[1:]

            assert len(moves) == len(abst_split)
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)

        elif 'CONTEXT :' in abst or 'AIMS :' in abst:
            ## 第九种模式
            moves = []
            for m in ['CONTEXT :','AIMS :','METHODS :','METHOD :','RESULTS :','RESULT :','CONCLUSIONS :','CONCLUSION:']:
                if m in abst:
                    moves.append(m)

            abst_split = re.split(r"CONTEXT :|AIMS :|METHODS? :|RESULTS? :|CONCLUSIONS? :",abst)[1:]

            assert len(moves) == len(abst_split)
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)

            
        elif 'CONTEXT :' in abst or 'AIMS :' in abst:
            ## 第十种模式
            moves = []
            for m in ['CONTEXT:','AIMS:','METHODS:','METHOD:','RESULTS:','RESULT:','CONCLUSIONS:','CONCLUSION:']:
                if m in abst:
                    moves.append(m)

            abst_split = re.split(r"CONTEXT:|AIMS:|METHODS?:|RESULTS?:|CONCLUSIONS?:",abst)[1:]

            assert len(moves) == len(abst_split)
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)

        elif 'CONTEXT.' in abst or 'AIMS.' in abst:
            ## 第十一种模式
            moves = []
            for m in ['CONTEXT.','AIMS.','METHODS.','METHOD.','RESULTS.','RESULT.','CONCLUSIONS.','CONCLUSION.']:
                if m in abst:
                    moves.append(m)

            abst_split = re.split(r"CONTEXT\.|AIMS\.|METHODS?\.|RESULTS?\.|CONCLUSIONS?\.",abst)[1:]

            assert len(moves) == len(abst_split)
            
            for j in range(len(moves)):
                df_dict['id'].append(id_)

                move = move_type_merge[moves[j].lower()]

                df_dict['move'].append(move)

                text = abst_split[j].strip()
                df_dict['text'].append(text)



    df_result = pd.DataFrame(df_dict)
    df_result.to_csv('result_test.csv')